TARGET_NAME = ZINO_VC_BL
TARGET_BOARD = ZINO_PREMIUM
SERIAL_PROT = COM5
MCPU = AT32F413RCT7
BUILD_DATE := $(shell date +"%Y%m%d")
MAIN_VERSION := 1
SUB_VERSION := 0
BUILD_NUMBER := 1
TARGET_VERSION = V$(MAIN_VERSION).$(SUB_VERSION).$(BUILD_NUMBER)
BUILD_VERSION += $(TARGET_VERSION)_$(BUILD_DATE)
TARGET_ELF = ./build/$(TARGET_NAME)_$(BUILD_VERSION).elf
TARGET_BIN = ./build/$(TARGET_NAME)_$(BUILD_VERSION).bin
FIRMWARE= $(TARGET_BOARD)_$(TARGET_NAME)_$(BUILD_VERSION).bin
ROOT     		:= $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
targets: $(zino_tops_lib) $(TARGET_BIN) $(TARGET_ELF) $(nOBJS)

linkerScript = $(ROOT)/libraries/cmsis/cm4/device_support/startup/gcc/linker/AT32F413xC_FLASH.ld
DSP_LIB_DIR = ./libraries/drivers/src
dspLibSrc= \
	./libraries/cmsis/cm4/device_support/system_at32f413.c \
	$(DSP_LIB_DIR)/at32f413_acc.c \
	$(DSP_LIB_DIR)/at32f413_adc.c \
	$(DSP_LIB_DIR)/at32f413_bpr.c \
	$(DSP_LIB_DIR)/at32f413_can.c \
	$(DSP_LIB_DIR)/at32f413_crc.c \
	$(DSP_LIB_DIR)/at32f413_crm.c \
	$(DSP_LIB_DIR)/at32f413_debug.c \
	$(DSP_LIB_DIR)/at32f413_dma.c \
	$(DSP_LIB_DIR)/at32f413_exint.c \
	$(DSP_LIB_DIR)/at32f413_flash.c \
	$(DSP_LIB_DIR)/at32f413_gpio.c \
	$(DSP_LIB_DIR)/at32f413_i2c.c \
	$(DSP_LIB_DIR)/at32f413_misc.c \
	$(DSP_LIB_DIR)/at32f413_pwc.c \
	$(DSP_LIB_DIR)/at32f413_rtc.c \
	$(DSP_LIB_DIR)/at32f413_sdio.c \
	$(DSP_LIB_DIR)/at32f413_spi.c \
	$(DSP_LIB_DIR)/at32f413_tmr.c \
	$(DSP_LIB_DIR)/at32f413_usart.c \
	$(DSP_LIB_DIR)/at32f413_usb.c \
	$(DSP_LIB_DIR)/at32f413_wdt.c \
	$(DSP_LIB_DIR)/at32f413_wwdt.c


PROJECT_DIR = ./project/at_start_f413/examples/usb_device/virtual_comport
PROJECT_SRC = \
	$(PROJECT_DIR)/src/main.c \
	$(PROJECT_DIR)/src/systick.c \
	$(PROJECT_DIR)/src/at32f413_int.c \
	$(PROJECT_DIR)/src/at32f413_clock.c \
	$(PROJECT_DIR)/src/AT32Models.c \
	$(PROJECT_DIR)/src/bootloader.c \
	./CRC/crc.c \
	./middlewares/usbd_drivers/src/usbd_core.c \
	./middlewares/usbd_drivers/src/usbd_int.c \
	./middlewares/usbd_drivers/src/usbd_sdr.c \
	./middlewares/usbd_class/cdc/cdc_class.c \
	./middlewares/usbd_class/cdc/cdc_desc.c



INCLUDES_DIR = \
	./libraries/cmsis/cm4/core_support \
	./libraries/cmsis/cm4/device_support \
	./libraries/drivers/inc \
	./project/at32f413_board \
	./middlewares/usbd_drivers/inc \
	./middlewares/usbd_class/cdc \
	$(PROJECT_DIR)/inc \
	./CRC

src_c = $(dspLibSrc) $(PROJECT_SRC)
DEPS = $(patsubst ./%,./build/%,$(patsubst %.c, %.d, $(src_c)))
OBJS = $(patsubst ./%,./build/%,$(patsubst %.c, %.o, $(src_c)))
INCLUDES = $(addprefix -I", $(addsuffix ", $(INCLUDES_DIR)))
SRC += $(src_c)
SRC += ./libraries/cmsis/cm4/device_support/startup/gcc/startup_at32f413.s
DEPS += ./build/libraries/cmsis/cm4/device_support/startup/gcc/startup_at32f413.d
OBJS += ./build/libraries/cmsis/cm4/device_support/startup/gcc/startup_at32f413.o


# $(info OBJS: $(OBJS))

# NOTE 所有头文件路径！
c_flags = \
	-DARDUINO -DZINO_RC_HOPPING -DAT_START_F413_V1\
	-mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 \
	-Os -ffunction-sections -fdata-sections \
	-Wall  -g -gdwarf-2 -DSOC_FAMILY_AT32 \
	-DSOC_SERIES_AT32F413 -DUSE_STDPERIPH_DRIVER -DAT32F413RCT7 \
	-lm -DARM_MATH_CM4 \
	-DDEV_MVID=$(MAIN_VERSION) -DDEV_SVID=$(SUB_VERSION) -DDEV_BVID=$(BUILD_NUMBER) \
	$(INCLUDES) \
	-std=gnu11 -MMD -MP -MD -MF "$(@:%.o=%.d)" -MT"$(@)"


zino_tops_lib = $(ROOT)/build/libTops.a
math_lib = libarm_cortexM4l_math
math_lib_dir = $(ROOT)/libraries/cmsis/lib/gcc/
DATE_TIME = $(shell date +"%Y-%m-%d-%H:%M:%S")
# NOTE -lc -lm 链接数学库时需要！!！最后！！！再链接，注意顺序
asm_flags = \
	-mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 \
	-Os -ffunction-sections -fdata-sections -Wall  -g -gdwarf-2 -x assembler-with-cpp \
	-I"$(ROOT)" \
	-Xassembler -mimplicit-it=thumb -MMD -MP

-include $(DEPS)

./build/%.o: %.c
	@ echo "$(DATE_TIME) $< ..."
	@ mkdir -p $(dir $@)
	@ arm-none-eabi-gcc $< -c -g -o $@ $(c_flags)

# arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O0 -ffunction-sections -fdata-sections -Wall  -g -gdwarf-2 -x assembler-with-cpp -I"D:\RT-ThreadStudio\workspace\ZINO_FC_V4" -Xassembler -mimplicit-it=thumb -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -c -o "$@" "$<"
./build/%.o: %.s
	@ echo "$(DATE_TIME) $< ..."
	@ mkdir -p $(dir $@)
	@ arm-none-eabi-gcc $(asm_flags) -MF"$(@:%.o=%.d)" -MT"$(@)" -c -o "$@" "$<"
./build/%.o: %.S
	@ echo "$(DATE_TIME) $< ..."
	@ mkdir -p $(dir $@)
	@ arm-none-eabi-gcc $(asm_flags) -MF"$(@:%.o=%.d)" -MT"$(@)" -c -o "$@" "$<"

$(zino_tops_lib): $(OBJS)
	@ echo "$(DATE_TIME) building static library $@  ..."
	@ mkdir -p $(dir $@)
	@ arm-none-eabi-ar rcs $@ $(OBJS)
m_ldflags= \
	-mcpu=cortex-m4 \
	-mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Os \
	-Wall  -g -gdwarf-2 \
	-T "$(linkerScript)" \
	-Wl,-Map,"./build/$(TARGET_NAME).map" \
	-Xlinker --gc-sections \
	-Xlinker --cref -Wl,--print-memory-usage

# arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O0 -ffunction-sections -fdata-sections -Wall  -g -gdwarf-2
# -T "D:\RT-ThreadStudio\workspace\ZINO_FC_V4\linkscripts\AT32F413RC\link.lds"
#  -Xlinker --gc-sections -Wl,-Map,"rtthread.map"
# -Xlinker --cref -o "rtthread.elf" $(OBJS) $(USER_OBJS) $(LIBS)
# $(TARGET_ELF): $(zino_tops_lib)
# 	@ echo "$(DATE_TIME) Linking everyting together... $@"
# 	@ arm-none-eabi-gcc $(m_ldflags) -o $(TARGET_ELF) $(nOBJS) -lc -lm
# 	@ arm-none-eabi-size $(TARGET_ELF)
$(TARGET_ELF): $(zino_tops_lib)
	@ echo "$(DATE_TIME) Linking everyting together... $@"
	arm-none-eabi-gcc -o $(TARGET_ELF) -Wl,--whole-archive $(zino_tops_lib) -Wl,--no-whole-archive $(m_ldflags) -lc -lm

cl:
	-$(RM) $(OBJS)$(TARGET_ELF)$(DEPS)
	-$(RM) -rf ./build
	-@echo ' '
$(TARGET_BIN): $(TARGET_ELF)
	@ echo "$(DATE_TIME) Create binary file... $@"
	@ arm-none-eabi-objcopy -O binary $(TARGET_ELF) $(TARGET_BIN)
	./win_iap_tool/makeFirmware.exe $(TARGET_BIN) $(FIRMWARE)
IAP_flash_$(TARGET): $(TARGET_BIN)
	chcp 65001
	./UploadTool/ZINO_IAP_WIN_V1.2.exe --serialdev $(SERIAL_PROT) --board $(TARGET_BOARD) --mcu $(MCPU) --id --version --write 0x08001800 --loadfile $(TARGET_BIN)

# dl: IAP_flash_$(TARGET)
dl:
	./win_iap_tool/win_iap_tool.exe $(SERIAL_PROT) 921600 ./AT32F413RCT7-ZINO-VirtualComport-bootloader-V1.0.0-20241115.bin

mix:
	cp $(zino_tops_lib) $(TARGET_MIXLYDIR)
	echo "$(DATE_TIME) Copy library to $(TARGET_MIXLYDIR) ..."



